<?php
namespace app\admin\controller;
use app\admin\model\Admin as AdminModel;
class Admin extends Common
{
    public function lit()
    {
        //select t1.id,t1.name,t2.title,t2.rules from bk_admin as t1 left join bk_rbac_group as t2 on t1.gid = t2.id;
    	$adminRes = db('admin') 
            -> alias('t1')     
            -> field('t1.id,t1.name,t2.title,t2.rules')
            -> join('bk_rbac_group t2','t1.gid = t2.id')
            -> select();
    	$this -> assign('adminRes',$adminRes);
        return $this -> fetch();
    }
    public function add()
    {
    	if(request() -> isPost()){
            $adminModel = new AdminModel;
    		$post = input('post.');
    		$res = $adminModel -> allowField(true) -> save($post);
    		if($res){
    			$this -> success('增加用户成功','lit',3);
    		}else{
    			$this -> error('增加用户失败');
    		}
    	}
        $groupRes = db('rbac_group') -> field('id,title') -> select();
        $this -> assign('groupRes',$groupRes);
        return $this -> fetch();
    }
    public function edit()
    {
    	$adminEdit = db('admin') -> find(input('id'));
    	if(request() -> isPost()){
    		$post = input('post.');
    		$post['password'] = $post['password'] ? $post['password'] : $adminEdit['password'];
    		if(db('admin') -> update($post) !== false){
    			$this -> success('编辑管理员资料成功','lit');
    		}else{
    			$this -> error('编辑管理员资料失败');
    		}
    		return ;
    	}
        $groupRes = db('rbac_group') -> field('id,title') -> select();
        $this -> assign('groupRes',$groupRes);
    	$this -> assign('adminEdit',$adminEdit);
        return $this -> fetch();
    }
    public function del(){
    	$b = db('admin') -> delete(input('id'));
    	if($b){
    		$this -> success('删除管理员成功');
    	}else{
    		$this -> error('删除管理员失败');
    	}
    }
}
